/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jhu.profiteer.data.exceptions.NonexistentEntityException;
import jhu.profiteer.data.exceptions.PreexistingEntityException;

/**
 *
 * @author Jeremy
 */
public class CurrentCommodityValueJpaController {

  public CurrentCommodityValueJpaController() {
    emf = Persistence.createEntityManagerFactory("profiteer-gamePU");
  }
  private EntityManagerFactory emf = null;

  public EntityManager getEntityManager() {
    return emf.createEntityManager();
  }

  public void create(CurrentCommodityValue currentCommodityValue)
          throws PreexistingEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      em.persist(currentCommodityValue);
      em.getTransaction().commit();
    } catch (Exception ex) {
      if(findCurrentCommodityValue(currentCommodityValue.getId().getCommodityId(),
              currentCommodityValue.getId().getCharacterId()) != null) {
        throw new PreexistingEntityException(currentCommodityValue +
                " already exists.", ex);
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void edit(CurrentCommodityValue currentCommodityValue)
          throws NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      currentCommodityValue = em.merge(currentCommodityValue);
      em.getTransaction().commit();
    } catch (Exception ex) {
      String msg = ex.getLocalizedMessage();
      if (msg == null || msg.length() == 0) {
        if (findCurrentCommodityValue(currentCommodityValue.getId().getCommodityId(),
                currentCommodityValue.getId().getCharacterId()) == null) {
          throw new NonexistentEntityException(currentCommodityValue +
                  " no longer exists.");
        }
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroy(Long commodityId, Long characterId)
          throws NonexistentEntityException {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Query q = em.createQuery("delete from CurrentCommodityValue as o" +
              " where o.id.commodityId = :commodityId" +
              " and o.id.characterId = :characterId")
              .setParameter("commodityId", commodityId)
              .setParameter("characterId", characterId);
      q.executeUpdate();
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroyWithCharacterId(Long characterId) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Query q = em.createQuery("delete from CurrentCommodityValue as o" +
              " where o.id.characterId = :characterId")
              .setParameter("characterId", characterId);
      q.executeUpdate();
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public List<CurrentCommodityValue> findCurrentCommodityValueEntities() {
    EntityManager em = getEntityManager();
    try {
      return em.createQuery("select object(o) from CurrentCommodityValue as o")
              .getResultList();
    } finally {
      em.close();
    }
  }

  public List<CurrentCommodityValue> findCurrentCommodityValuesByCharacterId(
          Long characterId) {
    EntityManager em = getEntityManager();
    try {
      return em.createQuery("select object(o) from CurrentCommodityValue as o" +
              " where o.id.characterId = :characterId")
              .setParameter("characterId", characterId)
              .getResultList();
    } finally {
      em.close();
    }
  }

  public CurrentCommodityValue findCurrentCommodityValue(Long commodityId,
          Long characterId) {
    EntityManager em = getEntityManager();
    try {
      return (CurrentCommodityValue)em
              .createQuery("select object(o) from CurrentCommodityValue as o" +
              " where o.id.commodityId = :commodityId" +
              " and o.id.characterId = :characterId")
              .setParameter("commodityId", commodityId)
              .setParameter("characterId", characterId)
              .getSingleResult();
    } finally {
      em.close();
    }
  }
}
